package com.beecloud.microservice.user.dao.constant;

import javaslang.Tuple;
import javaslang.Tuple2;
import javaslang.collection.List;


public final class UserTableConstant {
    private static final Tuple2 idCol = Tuple.of("id", "userId");
    private static final Tuple2  userNameCol =  Tuple.of("userName", "userName");
    private static final Tuple2  nickNameCol =  Tuple.of("nickName", "nickName");
    private static final Tuple2  emailCol =  Tuple.of("email", "email");
    private static final Tuple2  passwordCol =  Tuple.of("password", "password");
    private static final Tuple2  driverLicenseCol =  Tuple.of("driverLicense", "driverLicense");
    private static final Tuple2  phoneCol =  Tuple.of("phone", "phone");
    private static final String  tableName =  "user";
    private static final String propFormat = "#{%s}";
    @SuppressWarnings("unchecked")
    private static final List<Tuple2<String, String>> allCols = List.of(idCol, userNameCol, nickNameCol, emailCol, passwordCol, driverLicenseCol, phoneCol);

    private static final String allInsertCols = allCols.map((tu)-> tu._1).mkString(",");

    private static final String allInsertPros = allCols.map((tu)-> String.format(propFormat, tu._2)).mkString(",");


    private static final String selectedCols = allCols.map((tu) -> String.format("%s as %s", tu._1, tu._2)).mkString(",");

    public static final String insertSql = String.format("insert into %s(%s) values(%s)",  tableName, allInsertCols, allInsertPros);
    private static final String querySqlPrefix = String.format("select %s from %s", selectedCols, tableName);

    private static final String userIdsCondition = String.format(" where %s in (#{userIds})", idCol._1);

    private static final String userNameCondition = String.format(" where %s like #{userName}", userNameCol._1);

    public static final String queryByUserIdsSql = querySql(userIdsCondition);

    public static final String queryByUserNameSql = querySql(userNameCondition);


   private static String querySql(String condition) {
       return String.format("%s %s", querySqlPrefix, condition);
   }

   public static void main(String[] args) {
       System.out.println(insertSql);
   }

}
